 #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <cstring>

int main() {
    const char* filename = "disk_file";
    const size_t file_size = 32 * 1024 * 1024; // 32MB
    const size_t buffer_size = 4096; // 4KB缓冲区
    
    // 创建缓冲区，全部初始化为0
    char buffer[buffer_size];
    memset(buffer, 0, buffer_size);
    
    // 打开文件（创建或截断）
    int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd == -1) {
        std::cerr << "Error opening file: " << strerror(errno) << std::endl;
        return 1;
    }
    
    // 计算需要写入的次数
    size_t remaining_size = file_size;
    while (remaining_size > 0) {
        size_t write_size = (remaining_size < buffer_size) ? remaining_size : buffer_size;
        
        ssize_t bytes_written = write(fd, buffer, write_size);
        if (bytes_written == -1) {
            std::cerr << "Error writing to file: " << strerror(errno) << std::endl;
            close(fd);
            return 1;
        }
        
        remaining_size -= bytes_written;
    }
    
    // 关闭文件
    close(fd);
    
    std::cout << "Successfully created " << filename << " with size " << file_size << " bytes" << std::endl;
    return 0;
}